﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using EvaluationSystem.Exceptions;
using System.Web.UI;

namespace EvaluationSystem.DAO
{
    public enum Roles
    {
        总经理,
        副总经理1,
        副总经理2,
        小组经理,
        组员,
        系统管理员,
        文员,
        None
    }

    public enum Groups
    {
        资金组 = 1,
        预算组,
        投融资组,
        燃料组,
        GRIS产品运维团队,
        分析组,
        核算组,
        电价组,
        平台组,
        管理员组,
        None
    }

    public class LoginManager : DAO
    {
        const string CheckLoginCommand = "select [Groups],[RoleId] from [Users] where UserName=@UserName and Password=@Password";
        public string UserName { get; protected set; }
        public string Password { get; protected set; }
        public Roles Role { get; protected set; }
        public Groups[] Group { get; protected set; }
        public LoginManager(string userName, string password)
        {
            UserName = userName;
            Password = password;
        }

        private Roles GetRole(int roleId)
        {
            Roles role = Roles.None;
            string getRoleNameByRoleIdCommand = "select [RoleName] from [Roles] where [Id]=@Id";
            Dictionary<string, object> parameters = new Dictionary<string, object>(1);
            parameters["Id"] = roleId;
            DataTable dt = ExcuteCommandDataTable(getRoleNameByRoleIdCommand, parameters);
            if (dt.Rows.Count == 0)
            {
                return Roles.None;
            }
            string roleName = dt.Rows[0]["RoleName"] as string;
            if (roleName == "总经理")
            {
                role = Roles.总经理;
            }
            else if (roleName == "副总经理1")
            {
                role = Roles.副总经理1;
            }
            else if (roleName == "副总经理2")
            {
                role = Roles.副总经理2;
            }
            else if (roleName == "小组经理")
            {
                role = Roles.小组经理;
            }
            else if (roleName == "组员")
            {
                role = Roles.组员;
            }
            else if (roleName == "系统管理员")
            {
                role = Roles.系统管理员;
            }
            else if (roleName == "文员")
            {
                role = Roles.文员;
            }
            else
            {
                role = Roles.None;
            }
            return role;
        }

        private Groups[] GetGroup(string group)
        {
            List<Groups> groups = new List<Groups>();
            string[] theGroupIds = group.Split(";".ToArray());
            foreach (string g in theGroupIds)
            {
                groups.Add(ConverNameToGroup(g));
            }
            //string selectCommand = "select [GroupName] from [Groups] where [Id]=@Id0 ";
            //Dictionary<string,object> parameters = new Dictionary<string,object>(theGroupIds.Length);
            //parameters["Id0"] = theGroupIds[0];
            //for (int i = 1; i < theGroupIds.Length; i++)
            //{
            //    string p = "@Id"+i;
            //    selectCommand += " or [Id]="+p;
            //    parameters[p.Substring(1)] = theGroupIds[i];
            //}
            //DataTable dt = ExcuteCommandDataTable(selectCommand, parameters);
            //foreach (DataRow row in dt.Rows)
            //{
            //    groups.Add(ConverNameToGroup(row["GroupName"] as string));
            //}
            return groups.ToArray();
        }

        private Groups ConverNameToGroup(string groupName)
        {
            string[] groups = Enum.GetNames(typeof(Groups));
            byte i = 0;
            foreach (string g in groups)
            {
                i++;
                if (g.Equals(groupName))
                {
                    return (Groups)Enum.ToObject(typeof(Groups), i);
                }
                
            }
            return Groups.None;
        }

        public bool DoLogin(Page page)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>(2);
            parameters["Password"] = Utility.MD5(Password);
            parameters["UserName"] = UserName;
            DataTable dt = ExcuteCommandDataTable(CheckLoginCommand, parameters);
            bool isSuccess = dt.Rows.Count > 0;
            if (isSuccess)
            {
                try
                {
                    int roleId = (int)dt.Rows[0]["RoleId"];
                    Role = GetRole(roleId);
                    if (Role == Roles.None)
                    {
                        throw new RoleException();
                    }
                    Group = GetGroup(dt.Rows[0]["Groups"] as string);
                    page.Session[LoginConstants.UserName] = UserName;
                    page.Session[LoginConstants.Role] = Role;
                    page.Session[LoginConstants.Group] = Group;
                }
                catch(RoleException)
                {
                    throw;
                }
                catch { }
            }
            return isSuccess;
        }
    }
}